/*
 * Revenea 1.0
 * http://www.revenea.com
 */

Ext.onReady(function(){
    Ext.QuickTips.init();

    // the column model has information about grid columns
    // dataIndex maps the column to the specific data field in
    // the data store (created below)
    var cm = new Ext.grid.ColumnModel
    (
		[
			{
				header: "ID"
				,dataIndex: 'id'
				,width: 60
				,sortable: true
			}
			,{
				header: "Username"
				,dataIndex: 'username'
				,width: 150
				,sortable: true
				,editor: new Ext.form.TextField
				(
					{
						allowBlank: false
					}
				)
			}
			,{
				header: "Password"
				,dataIndex: 'password'
				,width: 150
				,editor: new Ext.form.TextField
				(
					{
						allowBlank: false
					}
				)
				/*
				,renderer: function(value, metadata, record, rowIndex, colIndex, store)
				{
					// console.log("For row " + rowIndex + ", column " + colIndex + ", the value of Password is " + value + ".");
					return value;
				}
				*/
			}
        ]
    );

    // by default columns are sortable
    cm.defaultSortable = true;


	// NOTE: At this point, any data classes, i.e. Ext.data.Record definitions/constructors, should be already included and defined.


    // create the Data Store
    var store = new Ext.data.JsonStore({
        url: '/com/revenea/account/service/User.cfc',
        baseParams: {method:'getAll', returnFormat:'JSON'},
        fields: User, // This comes from an external JavaScript class file, included by the HTML document
        sortInfo:{field:'username', direction:'ASC'}
	});

    // create the editor grid
    var grid = new Ext.grid.EditorGridPanel
    (
    	{
			store: store,
			cm: cm,
			renderTo: 'editor-grid',
			width:600,
			height:300,
			title:'Manage Users',
			frame:true,
			clicksToEdit:1
			,
			tbar: [{
			    text: 'Add User',
			    handler : function(){
			        var p = new User({
			            id: 0
			            ,username: ''
			            ,password: ''
			            ,isAuthenticated: false
			        });
			        grid.stopEditing();
			        store.insert(0, p);
			        grid.startEditing(0, 0);
			    }
			}]
		}
	);
	
	var foo = function(e) { console.debug(e); };
	grid.addListener("afteredit", foo);

    // trigger the data store load
    store.load();



});